app.controller("classpayController",function($scope, $http){
    // 超级管理员权限
    $http({
        method: 'GET',
        url: '/OA/staff/roles',
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
    }).success(function (response) {
        $scope.roles=[];
        $scope.roles=response.data;
        console.log($scope.roles)
        function permissions(){
            var rs=false;
            for (var i = 0; i < $scope.roles.length; i++) {
                if ($scope.roles[i] == 7) {
                    rs = true;
                }else{
                    rs= false;
                }
            }
            return rs
        }
        $scope.admin=permissions();
        console.log($scope.admin)
    })
    var listJson={};
    function changeJson(){
        var   campus_id=$("#schoolcampus").find("option:selected").val();
        return {campus_id:campus_id}
    }
    listJson=changeJson();
    $scope.find=function () {
        listJson= changeJson();
        applypeople();
    };
    applypeople();
    function applypeople() {
        $http({
            method: 'POST',
            url: '/OA/mend/approve?type=2',
            data: $.param(listJson),
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
        }).success(function (response) {
            console.log(response)
            $scope.total = response.data.length;
            //反转函数转化abcd转dcba
            $scope.data = response.data;
            //选择显示的条数
            $scope.values = [{"limit":6}];
            //默认显示的条数
            $scope.selectedLimit=$scope.values[0];
            //默认显示当前页数
            $scope.currentPage = 1;
            if($scope.data != null){
                $scope.datas = $scope.data.slice($scope.selectedLimit.limit*($scope.currentPage-1),$scope.selectedLimit.limit*$scope.currentPage);
            }else{
                alert($scope.data);
            }
            $scope.page = {
                "limit":$scope.selectedLimit.limit,"pageSize":5,"pageNo":$scope.currentPage,"totalCount":$scope.total};
            // 通过
            $scope.daily_adopt = function($index){
                if ($index>=0) {
                    if(confirm("是否通过"+$scope.datas[$index+(($scope.page.pageNo-1)*$scope.page.limit)].student_name+"的申请") ){
                        $http({
                            method:"POST",
                            url:'/OA/mend/approveSucceed?type=2',
                            data:$.param({id:$scope.datas[$index+(($scope.page.pageNo-1)*$scope.page.limit)].id}),
                            headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
                        }).success(function (result) {
                            if(result.ok==true){
                                setTimeout(function () {
                                    window.location.reload()
                                },1)
                            }else{
                                alert("申请失败,请重新申请!")
                            }
                        })
                    }
                };
            }
            // 驳回
            $scope.daily_reject = function($index){
                if ($index>=0) {
                    if(confirm("是否驳回"+$scope.datas[$index+(($scope.page.pageNo-1)*$scope.page.limit)].student_name+"的申请") ){
                        $http({
                            method:"POST",
                            url:'/OA/mend/approveFailure?type=2',
                            data:$.param({id:$scope.datas[$index+(($scope.page.pageNo-1)*$scope.page.limit)].id}),
                            headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
                        }).success(function (result) {
                            if(result.ok==true){
                                setTimeout(function () {
                                    window.location.reload()
                                },1)
                            }
                        })
                    }
                };
            }
        })
    }
            $scope.change = function(selectedLimit){
                $scope.page.limit = selectedLimit.limit;
                $scope.datas = $scope.data.slice($scope.selectedLimit.limit*($scope.page.pageNo-1),$scope.selectedLimit.limit*$scope.page.pageNo);
            }
            $scope.pageChanged = function(){
                $scope.page.limit = $scope.selectedLimit.limit;
                $scope.datas = $scope.data.slice($scope.selectedLimit.limit*($scope.page.pageNo-1),$scope.selectedLimit.limit*$scope.page.pageNo);
            }
            $scope.setPage = function (go) {
                $scope.length = Math.ceil($scope.total/$scope.selectedLimit.limit);
                console.log($scope.length);
                if(go > $scope.length){
                    $scope.page.pageNo =  $scope.length;
                    console.log($scope.length);
                    
                }else{
                    $scope.page.pageNo=go; 
                }
                $scope.datas = $scope.data.slice($scope.selectedLimit.limit*($scope.page.pageNo-1),$scope.selectedLimit.limit*$scope.page.pageNo);
            };



     
      
    });


